#include "accelerometer.h"

Accelerometer::Accelerometer(QObject *parent)
: QObject(parent), m_x(0), m_y(0), m_z(0), m_amplitude(0)
{
	m_sensor = new QAccelerometer(this);
	m_sensor->addFilter(this);
	m_sensor->start();
}

Accelerometer::~Accelerometer()
{
	m_sensor->stop();
	delete m_sensor;
}

bool Accelerometer::filter(QAccelerometerReading* reading)
{
	m_x = reading->x();
	m_y = reading->y();
	m_z = reading->z();
	m_amplitude = fabs(sqrt(m_x*m_x + m_y*m_y + m_z*m_z) - GRAVITY);

	emit(readingsChanged(m_x, m_y, m_z, m_amplitude));

	// to prevent flooding.
	// FPS ~ 60
	usleep(16000);

	return true;
}

void Accelerometer::read(double *x, double *y, double *z)
{
	*x = m_x;
	*y = m_y;
	*z = m_z;
}

SOUNDS Accelerometer::currentSound()
{
	SOUNDS current = NONE;

	if(m_x != 0){
		current = SWING3;
	}
	return current;
}

double Accelerometer::getAmplitude()
{
	return m_amplitude;
}
